<template>
  <div class="p-6px">
    <el-card class="rounded-md" shadow="never">
      <!-- 搜索条件 -->
      <el-form v-show="showSearch" :inline="true" id="searchFormId" @submit.prevent>
        <el-form-item label="商品名称" prop="name">
          <el-input placeholder="请输入商品名称" v-model="searchParams.name" clearable style="width: 200px"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="search" plain v-debounce="handleSearch">搜索</el-button>
          <el-button type="danger" icon="refresh" plain v-throttle="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 表格头部按钮 -->
      <el-row :gutter="10">
        <el-col :span="1.5" v-auth="['system:role:add']">
          <el-button type="primary" icon="plus" plain @click="handleAdd()">新增</el-button>
          <el-button type="primary" plain @click="handleAdd()">批量上架</el-button>
          <el-button type="danger" plain @click="handleAdd()">批量下架</el-button>
        </el-col>
        <KoiToolbar v-model:showSearch="showSearch" @refresh-table="handleListPage"></KoiToolbar>
      </el-row>

      <br />
      <!-- 数据表格 :data="tableList" -->
      <el-table v-loading="loading" v-adaptive border :data="tableList" empty-text="暂时没有数据哟"
        @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="序号" prop="id" width="80px" align="center" type="index"></el-table-column>
        <el-table-column label="商品名称" prop="name" width="120px" align="center"
          :show-overflow-tooltip="true"></el-table-column>
        <el-table-column label="商品图片" prop="avatar" width="100px" align="center">
          <template #default="scope">
            <div class="flex justify-center">
              <el-image class="w-72px h-72px" :preview-teleported="true" :preview-src-list="scope.row.slider_image"
                :src="scope.row.slider_image[0] != null && scope.row.slider_image[0] !== ''
                    ? scope.row.slider_image[0]
                    : 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png'
                  ">
                <template #error>
                  <el-icon class="c-[--el-color-primary]" :size="36">
                    <CircleCloseFilled />
                  </el-icon>
                </template>
              </el-image>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="商品分类" prop="unit" align="center" width="150px" :show-overflow-tooltip="true">
          <template #default="scope">
            {{ [scope.row.cate1?.name, scope.row.cate2?.name, scope.row.cate3?.name].filter(r => r).join(" / ") }}
          </template>
        </el-table-column>
        <el-table-column label="商品品牌" prop="brand.name" width="120px" align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column label="单位" prop="unit" align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column label="销量" prop="sales" align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column label="浏览量" prop="views" align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column label="收藏量" prop="collects" align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column label="是否多规格" prop="spec" align="center" width="120px" :show-overflow-tooltip="true">
          <template #default="scope">
            {{ scope.row.spec == 1 ? "单规格" : "多规格" }}
          </template>
        </el-table-column>
        <el-table-column label="销售价" prop="price" width="100px" align="center"></el-table-column>
        <el-table-column label="库存数" prop="stock" width="100px" align="center"></el-table-column>
        <el-table-column label="是否上架" prop="is_show" width="100px" align="center">
          <template #default="scope">
            {{ scope.row.spec == 1 ? "上架" : "下架" }}
          </template>
        </el-table-column>
        <el-table-column label="创建时间" prop="create_time" width="180px" align="center"></el-table-column>
        <el-table-column label="修改时间" prop="update_time" width="180px" align="center"></el-table-column>
        <el-table-column label="操作" align="left" width="350" fixed="right"
          v-auth="['system:role:update', 'system:role:delete']">
          <template #default="{ row }">
            <el-button type="primary" size="small" plain @click="handleUpdate(row)" v-auth="['system:role:update']">
              修改
            </el-button>
            <el-button type="primary" size="small" plain @click="handleDetail(row)" v-auth="['system:user:role']">
              详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <br />
      <!-- 分页 -->
      <el-pagination background v-model:current-page="searchParams.pageNo" v-model:page-size="searchParams.pageSize"
        v-show="total > 0" :page-sizes="[10, 20, 50, 100, 200]" layout="total, sizes, prev, pager, next, jumper"
        :total="total" @size-change="handleListPage" @current-change="handleListPage" />

      <KoiDrawer ref="koiDetailDrawerRef" title="商品详情" footer-hidden :size="'1150'">
        <template #content>
          <Detail :info="info"></Detail>
        </template>
      </KoiDrawer>
    </el-card>
  </div>
</template>

<script setup lang="ts" name="memberPage">
import { onMounted, ref } from "vue";
import { useRouter } from "vue-router";
import { koiNoticeError } from "@/utils/koi.ts";
import { read, listPage } from "@/api/pms/goods/index.ts";
import { koiDatePicker } from "@/utils/index.ts";
import Detail from "./Detail.vue";

// 数据表格加载页面动画
const loading = ref(false);
/** 是否显示搜索表单 */
const showSearch = ref<boolean>(true); // 默认显示搜索条件
// 数据表格数据
const tableList = ref<any>([]);
// 查询参数
const searchParams = ref({
  pageNo: 1, // 第几页
  pageSize: 10, // 每页显示多少条
  name: ""
});

const total = ref<number>(0);
const router = useRouter();
// 重置搜索参数
const resetSearchParams = () => {
  searchParams.value = {
    pageNo: 1,
    pageSize: 10,
    name: ""
  };
  dateRange.value = [];
};

/** 搜索 */
const handleSearch = () => {
  searchParams.value.pageNo = 1;
  handleListPage();
};

/** 重置 */
const resetSearch = () => {
  resetSearchParams();
  handleListPage();
};

// 时间
const dateRange = ref();
/** @current-change：点击分页组件页码发生变化：例如：切换第2、3页 OR 上一页 AND 下一页 OR 跳转某一页 */
/** @size-change：点击分页组件下拉选中条数发生变化：例如：选择10条/页、20条/页等 */
// 分页查询，@current-change AND @size-change都会触发分页，调用后端分页接口
/** 数据表格 */
const handleListPage = async () => {
  try {
    loading.value = true;
    tableList.value = []; // 重置表格数据
    const res: any = await listPage(koiDatePicker(searchParams.value, dateRange.value, "created"));
    tableList.value = res.data.records;
    total.value = res.data.total;
    loading.value = false;
  } catch (error) {
    console.log(error);
    koiNoticeError("数据查询失败，请刷新重试");
  }
};

onMounted(() => {
  // 获取数据表格数据
  handleListPage();
});

const ids = ref([]); // 选中数组
const single = ref<boolean>(true); // 非单个禁用
const multiple = ref<boolean>(true); // 非多个禁用
/** 是否多选 */
const handleSelectionChange = (selection: any) => {
  // console.log(selection);
  ids.value = selection.map((item: any) => item.id);
  single.value = selection.length != 1; // 单选
  multiple.value = !selection.length; // 多选
};

/** 添加 */
const handleAdd = () => {
  router.push({ path: "/pms/goods/edit" });
};

/** 修改 */
const handleUpdate = async (row?: any) => {
  router.push({ path: "/pms/goods/edit", query: { id: row.id } });
};
const info = ref();
const koiDetailDrawerRef = ref();

const handleDetail = (row: any) => {
  read(row.id).then(res => {
    info.value = res.data;
    koiDetailDrawerRef.value.koiOpen();
  });
};
</script>

<style lang="scss" scoped></style>
